<jsp:include page="header.jsp">
			<jsp:param value="item2" name="item"/>
</jsp:include> 
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<section class="content">
	<section class="widget"  id="standard" >
		<header>
			<span class="icon">&#128196;</span>
			<hgroup>
				<h1>信息列表</h1>
				<h2>差旅标准信息列表</h2>
			</hgroup>
		</header>
		<div class="content">
	<div class="tabs" id="tabs">
	    <ul>
	        <li class="on">住宿档次</li>
	        <li>交通方式</li>
	        <li>差旅标准</li>
	    </ul>
	    <div id="list">
		    <div id="hotellist">
		    	<div id="hotelitem"></div>
				<div id="hotelpagelist" class="pagelist"></div>
		    </div>
		    
		    <div class="hide" id="trafficlist">  
		    	<div id="trafficitem"></div>
				<div id="trafficpagelist" class="pagelist"></div>
		    </div>
		    
		    <div class="hide" id="tslist">
		    	<div id="tsitem"></div>
				<div id="tspagelist" class="pagelist"></div>
		    </div>
		</div>
	</div>
	
	<div id="hotelpopup" class="hide">
			<form action='' name='hotelform' id='hotelform'>
				<table id="myTable">
					<tr>
						<td>
							级别
						</td>
						<td>
							<input name="hotel" id="hotel" type="text">
							<br/>
							<font color="#dd0000">请输入该方式的等级</font>
						</td>
					</tr>
					<tr>
						<td>
							价格(元/天)
						</td>
						<td>
							<input name="hotelprice" id="hotelprice" type="text" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')">
							<br/>
							<font color="#dd0000">请输入最多两位小数的数字</font>
						</td>
					</tr>
				</table>
				<input type="button" value="确定"  class ="white" onclick="addinfo(0)"/>
			</form>
		</div>
		
		<div id="trapopup" class="hide">
			<form action='' name='traform' id='traform'>
				<table  id="myTable">
					<tr>
						<td>
							方式
						</td>
						<td>
							<input name="traffic" id="traffic" type="text">
							<br/>
							<font color="#dd0000">请输入出行的方式</font>
						</td>
					</tr>
					<tr>
						<td>
							级别
						</td>
						<td>
							<input name="standard" id="standard" type="text">
							<br/>
							<font color="#dd0000">请输入该方式的等级</font>
						</td>
					</tr>
					<tr>
						<td>
							价格(元/公里)	
						</td>
						<td>
							<input name="traprice" id="traprice" type="text" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')">
							<br/>
							<font color="#dd0000">请输入最多两位小数的数字</font>
						</td>
					</tr>
				</table>
				<input type="button" value="确定"  class ="white"  onclick="addinfo(1)"/>
			</form>
		</div>
		
		<div id="tspopup" class="hide">
			<form action='' name='tsform' id='tsform'>
				<table  id="myTable">
					<tr>
						<td>
							级别
						</td>
						<td>
							<input name="grade" id="grade" type="text">
							<br/>
							<font color="#dd0000">请输入该标准的等级</font>
						</td>
					</tr>
					<tr>
						<td>
							餐饮费/天
						</td>
						<td>
							<input name="mealsCost" id="mealsCost" type="text" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"> 
							<br/>
							<font color="#dd0000">请输入最多两位小数的数字</font>
						</td>
					</tr>
					<tr>
						<td>
							其他费用/天
						</td>
						<td>
							<input name="elseCost" id="elseCost" type="text" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')">
							<br/>
							<font color="#dd0000">请输入最多两位小数的数字</font>
						</td>
					</tr>
					<tr>
						<td>
							住宿档次
						</td>
						<td>
							<select name="hotelselect" id="hotelselect">
							</select>
						</td>
					</tr>
					<tr>
						<td>
							交通方式
						</td>
						<td>
							<select name="traselect" id="traselect">
							</select>
						</td>
					</tr>
				</table>
				<input type="button" value="确定"  class ="white"  onclick="addinfo(2)"/>
			</form>
		</div>
			</div>
	</section>
	
	<script type="text/javascript">
		//分页
		var hopages;
		var hocurr = 1;
		var trapages;
		var tracurr = 1;
		var tspages;
		var tscurr = 1;
		//
	
		var hotelstandard = [];
		var trafficstandard = [];
		window.onload = function(){
		   getinfodata(0,hocurr);	//一开始加载页面。。。
			
           var oTab = document.getElementById("tabs");
           var oUl = oTab.getElementsByTagName("ul")[0];
           var oLis = oUl.getElementsByTagName("li");
           //var oDivs= oTab.getElementsByTagName("div");
           //$("#tabs").not("")
           var oDivs = document.getElementById("list").children;

           for(var i= 0,len = oLis.length;i<len;i++){	  
               oLis[i].index = i;
               oLis[i].onclick = function() {
                   for(var n= 0;n<len;n++){
                       oLis[n].className = "";
                       oDivs[n].className = "hide";//隐藏的类名
                   }
	               this.className = "on";//显示的类名
	               oDivs[this.index].className = "";
	               var index = this.index;
	               if(index=='0'){
		           		getinfodata(this.index,hocurr);
		           }else if(index=='1'){
		           		getinfodata(this.index,tracurr);
		           }else if(index=='2'){
		               	getinfodata(this.index,tscurr);
		           }
               };
           }
        }
	
	function getinfodata(type,page){
		$.ajax( {
			url: "travelinfo!showInfoList.action",
			type: "get",
			data: {"type": type,"page": page},	//获取li的属性？可以传递的
			dataype: "json",
			contentType: "application/json",
			success: function(jsonResult){
				showInfo(type,jsonResult);	//这个也是
			},
			error: function(){
				alert("fail");
			}
	   });
	}
	
	function showInfo(index,jsonResult){
		var data = eval("("+jsonResult+")");
		var result="";
		var reg="/[^\d{1,}\\.\\d{1,}|\\d{1,}]/g,''";
		if(data==null){
			alert('null');
			return ;
		}
		if(index=='0'){
			hocurr = data.pagenum;
			hopages = data.pages;
			var list = data.hotellist;
			result+="<input type='button' value='添加'   class ='white' onclick='showWin(0);'>";
			result+="<table cellspacing='0'   align='center' id='myTable'>";
			result+="<tr>";
			result+="<th>ID</th>";
			result+="<th>级别</th>";
			result+="<th>价格(元/天)</th>";
			result+="<th>修改</th>";
			result+="<th>删除</th>";
			result+="</tr>";
			for(var i=0;i<list.length;i++){
				result+="<tr>";
				result+="<td><input type='text' readonly='true' value='"+list[i].hotelId+"' /></td>";
				result+="<td><input type='text' value='"+list[i].hotel+"' /></td>";
				result+='<td><input type="text" value="'+list[i].price+'" onkeyup="value=value.replace('+reg+')" /></td>';
				result+="<td><input type='button' name='update'  class ='white' value='修改' onclick=updateinfo(this,0,"+list[i].hotelId+") /></td>";
				result+="<td><input type='button' name='del'  class ='white' value='删除'  onclick=deleteinfo(0,"+list[i].hotelId+") /></td>";
				result+="</tr>";
			}
			result+="</table>";
			$("#hotelitem").html(result);
			
			laypage({
				cont: 'hotelpagelist',	//id
				pages: hopages,	//总页数
				curr: hocurr || 1,
				jump: function(obj,first){
					if(!first){
						getinfodata(0,obj.curr);
					}
				}
			});
			
		}else if(index=="1"){
			tracurr = data.pagenum;
			trapages = data.pages;
			var list = data.trafficlist;
			result+="<input type='button' value='添加'  class ='white' onclick='showWin(1);'>";
			result+="<table cellspacing='0'  align='center' id='myTable'>";
			result+="<tr>";
			result+="<th>ID</th>";
			result+="<th>方式</th>";
			result+="<th>级别</th>";
			result+="<th>价格(元/公里)</th>";
			result+="<th>修改</th>";
			result+="<th>删除</th>";
			result+="</tr>";
			for(var i=0;i<list.length;i++){
				result+="<tr>";
				result+="<td><input type='text' readonly='true' value='"+list[i].trafficId+"' /></td>";
				result+="<td><input type='text' value='"+list[i].traffic+"' /></td>";
				result+="<td><input type='text' value='"+list[i].standard+"' /></td>";
				result+='<td><input type="text" value="'+list[i].price+'" onkeyup="value=value.replace('+reg+')" /></td>';
				result+="<td><input type='button' name='update'  class ='white' value='修改' onclick=updateinfo(this,1,"+list[i].trafficId+") /></td>";
				result+="<td><input type='button' name='del'  class ='white' value='删除'  onclick=deleteinfo(1,"+list[i].trafficId+") /></td>";
				result+="</tr>";
			}
			result+="</table>";
			$("#trafficitem").html(result);
			
			laypage({
				cont: 'trafficpagelist',
				pages: trapages,
				curr: tracurr || 1,
				jump: function(obj,first){
					if(!first){
						getinfodata(1,obj.curr);
					}
				}
			});
			
		}else if(index=="2"){
			tscurr = data.pagenum;
			tspages = data.pages;
		
			//赋值给全局变量
			hotelstandard = data.hotellist;
			trafficstandard = data.trafficlist;
			
			result+="<input type='button' value='添加'  class ='white' onclick='showWin(2);'>";
			result+="<table align='center' id='myTable'>";
			result+="<tr>";
			result+="<th>ID</th>";
			result+="<th>级别</th>";
			result+="<th>餐饮费/天</th>";
			result+="<th>其他费用/天</th>";
			result+="<th>住宿ID</th>";
			result+="<th>交通方式ID</th>";
			result+="<th>修改</th>";
			result+="<th>删除</th>";
			result+="</tr>";
			
			for(var i=0;i<data.tslist.length;i++){
				result+="<tr>";
				result+="<td><input type='text' readonly='true' value='"+data.tslist[i].standardId+"' /></td>";
				result+="<td><input type='text' value='"+data.tslist[i].grade+"' /></td>";
				result+='<td><input type="text" value="'+data.tslist[i].mealsCost+'" onkeyup="value=value.replace('+reg+')" /></td>';
				result+='<td><input type="text" value="'+data.tslist[i].elseCost+'" onkeyup="value=value.replace('+reg+')" /></td>';
				//data.tslist[i].hotel.hotelId,data.tslist[i].traffic.trafficId
				result+="<td><select name='hotellist' id='hotellist'>";
				for(var j=0;j<data.hotellist.length;j++){
					if(data.hotellist[j].hotelId==data.tslist[i].hotel.hotelId){	//该差旅标准的hotelId作为默认值
						result+="<option value='"+data.hotellist[j].hotelId+"' selected='true'>"+data.hotellist[j].hotel+"</option>";
					}else{
						result+="<option value='"+data.hotellist[j].hotelId+"'>"+data.hotellist[j].hotel+"</option>";
					}
				}		
				result+="</select></td>";
				result+="<td><select name='trafficlist' id='trafficlist'>";
				for(var j=0;j<data.trafficlist.length;j++){	
					if(data.trafficlist[j].trafficId==data.tslist[i].traffic.trafficId){ //同上
						result+="<option value='"+data.trafficlist[j].trafficId+"' selected='true'>"+data.trafficlist[j].traffic+","+data.trafficlist[j].standard+"</option>";
					}else{
						result+="<option value='"+data.trafficlist[j].trafficId+"'>"+data.trafficlist[j].traffic+","+data.trafficlist[j].standard+"</option>";
					}
				}
				result+="</select></td>";
				result+="<td><input type='button' name='update'  class ='white' value='修改' onclick=updateinfo(this,2,"+data.tslist[i].standardId+") /></td>";
				result+="<td><input type='button' name='del'  class ='white' value='删除'  onclick=deleteinfo(2,"+data.tslist[i].standardId+") /></td>";
				result+="</tr>";
			}
			result+="</table>";
			$("#tsitem").html(result);
			
			laypage({
				cont: 'tspagelist',
				pages: tspages,
				curr: tscurr || 1,
				jump: function(obj,first){
					if(!first){
						getinfodata(2,obj.curr);
					}
				}
			});
			
		}
	}
	
	function deleteinfo(type,id){
		$.ajax({
			url: "travelinfo!deleteInfo.action",
			type: "post",
			data: {"type":type,"id":id},
			dataType: "json",
			contentType: "application/x-www-form-urlencoded;charset=UTF-8",
			success: function(message){
				if(message=="success"){
					alert("删除成功！");
					getinfodata(type,1);
				}else{
					alert("失败！");
				}
			},
			error: function(){
				alert("fail");
			}			
		});
	}
	
	function updateinfo(obj,type,id){
		var childNodes = obj.parentNode.parentNode.childNodes;	//td
		var data;
		var reg=/^-?\d+\.?\d{0,2}$/;
		switch(type){
			case 0: var hotelId = childNodes[0].firstChild.value;	//hotel
					var hotel = childNodes[1].firstChild.value;
					var price = childNodes[2].firstChild.value;
					//if(price=="")
						//price=0;
					if(!reg.test(price)){
						alert("请输入范围在000000000.00~999999999.99的至多两位小数的数字");
						return false;
					}
					data = {"type":type,"hotel.hotelId":hotelId,"hotel.hotel":hotel,"hotel.price":price};
					break;
			case 1: var trafficId = childNodes[0].firstChild.value;
					var traffic = childNodes[1].firstChild.value;
					var standard = childNodes[2].firstChild.value;
					var price = childNodes[3].firstChild.value;
					//if(price=="")
						//price=0;
					if(!reg.test(price)){
						alert("请输入范围在000000000.00~999999999.99的至多两位小数的数字");
						return false;
					}
					data = {"type":type,"traffic.trafficId":trafficId,"traffic.traffic":traffic,"traffic.standard":standard,"traffic.price":price};
					break;
			case 2: var standardId = childNodes[0].firstChild.value;
					var grade = childNodes[1].firstChild.value;
					var mealsCost = childNodes[2].firstChild.value;
					var elseCost = childNodes[3].firstChild.value;
					var hotelId = childNodes[4].firstChild.options[childNodes[4].firstChild.selectedIndex].value;	//firstChild--select
					var trafficId = childNodes[5].firstChild.options[childNodes[5].firstChild.selectedIndex].value;	
					/*if(mealsCost=="")
						mealsCost=0;
					if(elseCost=="")
						elseCost=0;	*/
					if(!reg.test(mealsCost)||!reg.test(elseCost)){
						alert("请输入范围在000000000.00~999999999.99的至多两位小数的数字");
						return false;
					}
					data = {"type":type,"travelStandard.standardId":standardId,"travelStandard.grade":grade,"travelStandard.mealsCost":mealsCost,"travelStandard.elseCost":elseCost,"travelStandard.hotel.hotelId":hotelId,"travelStandard.traffic.trafficId":trafficId};
		
		}
		$.ajax({
			url: "travelinfo!updateInfo.action",
			type: "post",
			data: data,
			dataType: "json",
			contentType: "application/x-www-form-urlencoded;charset=UTF-8",
			success: function(message){
				if(message!="")	alert(message);
				else{
					alert("修改成功！");
					getinfodata(type,1);
				}
			},
			error: function(){
				alert("fail");
			}
		});
	}
	
	function addinfo(type){
		var data;
		var reg=/^-?\d+\.?\d{0,2}$/;
		switch(type){
			case 0:	var hotel = $("input[name='hotel']").val();	//hotel
					var price = $("input[name='hotelprice']").val();
					//if(price=="")
						//price=0;
					if(!reg.test(price)){
						alert("请输入范围在000000000.00~999999999.99的至多两位小数的数字");
						return false;
					}
					data = {"type":type,"hotel.hotel":hotel,"hotel.price":price};
					break;
			case 1: var traffic = $("input[name='traffic']").val();	//traffic
					var standard = $("input[name='standard']").val();
					var price = $("input[name='traprice']").val();
					//if(price=="")
						//price=0;
					if(!reg.test(price)){
						alert("请输入范围在000000000.00~999999999.99的至多两位小数的数字");
						return false;
					}
					data = {"type":type,"traffic.traffic":traffic,"traffic.standard":standard,"traffic.price":price};
					break;
			case 2: var grade = $("input[name='grade']").val();	//trafficstandard
					var mealsCost = $("input[name='mealsCost']").val();
					var elseCost = $("input[name='elseCost']").val();
					var hotelId = $("#hotelselect option:selected").val();
					var trafficId = $("#traselect option:selected").val();
					/*if(mealsCost=="")
						mealsCost=0;
					if(elseCost=="")
						elseCost=0;*/
					if(!reg.test(mealsCost)||!reg.test(elseCost)){
						alert("请输入范围在000000000.00~999999999.99的至多两位小数的数字");
						return false;
					}
					data = {"type":type,"travelStandard.grade":grade,"travelStandard.mealsCost":mealsCost,"travelStandard.elseCost":elseCost,"travelStandard.traffic.trafficId":trafficId,"travelStandard.hotel.hotelId":hotelId};
					break;
		}
		
		$.ajax({
			url: "travelinfo!addInfo.action",
			type: "post",
			data: data,
			dataType: "json",
			contentType: "application/x-www-form-urlencoded;charset=UTF-8",
			success: function(message){
				if(message!="")	alert(message);
				else{
					alert("增添成功！");
					switch(type){
						case 0:	$("input[name='hotel']").val("");
								$("input[name='hotelprice']").val("");
								break;
						case 1:	$("input[name='traffic']").val("");	//traffic
								$("input[name='standard']").val("");
								$("input[name='traprice']").val("");
								break;
						case 2:	$("input[name='grade']").val("");	//trafficstandard
								$("input[name='mealsCost']").val("");
								$("input[name='elseCost']").val("");
								break;
					}
					getinfodata(type,1);
				}
			},
			error: function(){
				alert("error");
			}
		});
	}
	
	function showWin(type){	//类型
		var popup;
		switch(type){
			case 0: layer.open({
					        type: 1,
					        skin:'layui-layer-lan',
					        area: ['360px', '270px'],
					        shadeClose: true, //点击遮罩关闭
					        content:$('#hotelpopup')
					    });
					break;
			case 1: layer.open({
					        type: 1,
					        skin:'layui-layer-lan',
					        area: ['360px', '360px'],
					        shadeClose: true, //点击遮罩关闭
					        content:$('#trapopup')
					    });
					break;
			case 2:layer.open({
					        type: 1,
					        skin:'layui-layer-lan',
					        area: ['360px', '480px'],
					        shadeClose: true, //点击遮罩关闭
					        content:$('#tspopup')
					    });
					    
					//生成option
					//hotelstandard,trafficstandard
					
					var hotelselect = document.getElementById("hotelselect");
					var traselect = document.getElementById("traselect");
					
					//删除options
					hotelselect.options.length=0;
					traselect.options.length=0;
					
					for(var i=0;i<hotelstandard.length;i++){
						hotelselect.add(new Option(hotelstandard[i].hotel, hotelstandard[i].hotelId));
					}
					for(var i=0;i<trafficstandard.length;i++){
						traselect.add(new Option(trafficstandard[i].traffic+","+trafficstandard[i].standard, trafficstandard[i].trafficId));
					}
					break;
		}
		
	}
/* 			function resizeInput() {
   			 	$(this).attr('size', $(this).val().length);
			}
			$("input[type=text]").each(resizeInput); */
			
</script>
<%@ include file="footer.jsp" %>